package com.csxy.zqm.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;
import com.csxy.zqm.exception.MyException;
import com.csxy.zqm.service.VodService;
import com.csxy.zqm.util.ConstantVodUtils;

import com.csxy.zqm.util.InitObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;




@Service
public class VodServiceImpl implements VodService {
    @Override
    public String uploadAliyunVideo(MultipartFile file) {
        String fileName = file.getOriginalFilename();
//            title：上传之后显示名称
        String title = fileName.substring(0, fileName.lastIndexOf("."));
//            inputStream：上传
        try{
            InputStream inputStream = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(ConstantVodUtils.ACCESSKEY_ID
                    , ConstantVodUtils.ACCESSKEY_SECRET
                    , title
                    , fileName, inputStream);
            UploadVideoImpl uploadVideo = new UploadVideoImpl();
            UploadStreamResponse response = uploadVideo.uploadStream(request);
            System.out.println("RequsetId=" + response.getRequestId() + "\n");
            String videoId = null;
            if (response.isSuccess()){
                videoId = response.getVideoId();
                System.out.println("VideoId=" + getPlayUrl(videoId));
            }else {
                videoId = response.getVideoId();
                System.out.println("VideoId=" + getPlayUrl(videoId));
            }
            return videoId;


        } catch (IOException e){
            throw new MyException(20001, "上传文件失败");
        }

    }
//           根据ID删除
    @Override
    public void removeVideoAliyunById(String id) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(
                    ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET
            );
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(id);
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (ClientException e) {
            throw new MyException(20001, "视频删除失败");
        }

    }
//           根据ID批量删除
    @Override
    public void removeMoreVideo(List<String> videoIdList) {
            String str = StringUtils.join(videoIdList.toArray(), ",");

        try {

            DefaultAcsClient client = InitObject.initVodClient(
                    ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET
            );
            DeleteVideoRequest request = new DeleteVideoRequest();
            request.setVideoIds(str);
            DeleteVideoResponse response = client.getAcsResponse(request);
        } catch (Exception e) {
            throw new MyException(2001, "视频删除失败");
        }

    }

    public String getPlayUrl(String videoId) {
        try {
            DefaultAcsClient client = InitObject.initVodClient(ConstantVodUtils.ACCESSKEY_ID,
                    ConstantVodUtils.ACCESSKEY_SECRET);
            GetPlayInfoRequest request = new GetPlayInfoRequest();
            request.setVideoId(videoId);
            GetPlayInfoResponse response = client.getAcsResponse(request);
            return response.getPlayInfoList().get(0).getPlayURL();
        } catch (ClientException e) {
            throw new MyException(20001, "获取播放地址失败");
        }
    }



}
